﻿Namespace Cues
	''' <summary>
	''' A transport cue stops a specific cue or all the active cues.
	''' </summary>
	''' <remarks></remarks>
	<Serializable> Public Class Transport
		Inherits CueBase

		''' <summary>
		''' Represents the Action Property.
		''' </summary>
		''' <remarks></remarks>
		Private ActionProperty As TransportActions = My.Settings.DefaultTransportCueAction

		''' <summary>
		''' Indicates what action this cue will take.
		''' </summary>
		''' <value></value>
		''' <returns></returns>
		''' <remarks></remarks>
		Public Property Action As TransportActions
			Get
				Return ActionProperty
			End Get
			Set(value As TransportActions)
				If ActionProperty <> value Then
					ActionProperty = value
					RaisePropertyChanged("Action")
				End If
			End Set
		End Property

		''' <summary>
		''' Represents the TargetCue Property.
		''' </summary>
		''' <remarks></remarks>
		Private TargetCueProperty As Double = 1

		''' <summary>
		''' Represents the target cue if this cue will stop a specific cue.
		''' </summary>
		''' <remarks></remarks>
		Public Property TargetCue As Double
			Get
				Return TargetCueProperty
			End Get
			Set(value As Double)
				TargetCueProperty = value
				RaisePropertyChanged("TargetCue")
			End Set
		End Property

		''' <summary>
		''' Represents the TargetPan property
		''' </summary>
		''' <remarks></remarks>
		Private TargetPanProperty As Double = 0

		''' <summary>
		''' The ending value of the audio cue's pan property if the action is AutoPan.
		''' </summary>
		''' <value></value>
		''' <returns></returns>
		''' <remarks></remarks>
		Public Property TargetPan As Double
			Get
				Return TargetPanProperty
			End Get
			Set(value As Double)
				If TargetPanProperty <> value Then
					TargetPanProperty = value
					RaisePropertyChanged("TargetPan")
				End If
			End Set
		End Property

		''' <summary>
		''' Represents the TargetFade property.
		''' </summary>
		''' <remarks></remarks>
		Private TargetFadeProperty As Double = 0.5

		''' <summary>
		''' The ending value of the audio cue's volume property if the action is AutoFade.
		''' </summary>
		''' <value></value>
		''' <returns></returns>
		''' <remarks></remarks>
		Public Property TargetFade As Double
			Get
				Return TargetFadeProperty
			End Get
			Set(value As Double)
				If TargetFadeProperty <> value Then
					TargetFadeProperty = value
					RaisePropertyChanged("TargetFade")
				End If
			End Set
		End Property

		''' <summary>
		''' Represents the PanDuration property.
		''' </summary>
		''' <remarks></remarks>
		Private PanDurationProperty As Double = 5

		''' <summary>
		''' The duration (in seconds) over which the pan will occur if the action is AutoPan.
		''' </summary>
		''' <value></value>
		''' <returns></returns>
		''' <remarks></remarks>
		Public Property PanDuration As Double
			Get
				Return PanDurationProperty
			End Get
			Set(value As Double)
				If PanDurationProperty <> value Then
					PanDurationProperty = value
					RaisePropertyChanged("PanDuration")
				End If
			End Set
		End Property

		''' <summary>
		''' Represents the FadeDuration property.
		''' </summary>
		''' <remarks></remarks>
		Private FadeDurationProperty As Double = 5

		''' <summary>
		''' The duration (in seconds) over which the fade will occur if the action is AutoFade.
		''' </summary>
		''' <value></value>
		''' <returns></returns>
		''' <remarks></remarks>
		Public Property FadeDuration As Double
			Get
				Return FadeDurationProperty
			End Get
			Set(value As Double)
				If FadeDurationProperty <> value Then
					FadeDurationProperty = value
					RaisePropertyChanged("FadeDuration")
				End If
			End Set
		End Property

		''' <summary>
		''' Returns a string representation of this object.
		''' </summary>
		''' <returns></returns>
		''' <remarks></remarks>
		Public Overrides Function ToString() As String
			Return "Transport Cue: " & FullNumber
		End Function

        ''' <summary>
        ''' When called this cue will perform it's selected action.
        ''' </summary>
        ''' <remarks></remarks>
        Public Overrides Async Sub Start()
			Me.Status = CueStatus.Active
			If Action = TransportActions.StopAllActiveCues Then
				My.Application.Show.Console.SendMessage("Transport Cue #" & Number & " is trying to stop all active cues.")
				'Await My.Application.Show.StopAllCues(True)
				Await My.Application.Show.ShowFile.CueList.StopAllCues(True)
			ElseIf Action = TransportActions.StopSpecificCue Then
				If TargetCue > 0 Then
					Try
						My.Application.Show.Console.SendMessage("Transport Cue #" & Number & " is trying to stop the Cue #" & TargetCue)
						'My.Application.Show.StopCueByNumber(Me, TargetCue)
						My.Application.Show.ShowFile.CueList.FindCuesList(Me.FullNumber).FindCueByNumber(TargetCue).Stop()
						My.Application.Show.Console.SendMessage("Transport Cue #" & Number & " stopped the Cue #" & TargetCue)
					Catch ex As Exception
						My.Application.Show.Console.SendMessage("Transport Cue #" & Number & " could not stop the cue #" & TargetCue, ex)
						Return
					End Try
				End If
			ElseIf Action = TransportActions.StartCue Then
				If TargetCue > 0 Then
					Try
						My.Application.Show.Console.SendMessage("Transport Cue #" & Number & " is attempting to start the Cue #" & TargetCue)
						'My.Application.Show.StartCueByNumber(Me, TargetCue)
						Dim ql As Collections.CueList = My.Application.Show.ShowFile.CueList.FindCuesList(Me.FullNumber)
						If ql IsNot Nothing Then
							ql.StartCue(ql.FindCueByNumber(TargetCue))
						End If
					Catch ex As Exception
						My.Application.Show.Console.SendMessage("Transport Cue #" & Number & " could not start the cue #" & TargetCue, ex)
					End Try
				End If
			ElseIf Action = TransportActions.BumpCueStatus Then
				If TargetCue > 0 Then
					Try
						My.Application.Show.Console.SendMessage("Transport Cue #" & Number & " is attempting to bump the status of Cue #" & TargetCue)
						'My.Application.Show.BumpCueStatusByNumber(Me, TargetCue)
						My.Application.Show.ShowFile.CueList.FindCuesList(Me.FullNumber).FindCueByNumber(TargetCue).BumpStatus()
					Catch ex As Exception
						My.Application.Show.Console.SendMessage("Transport Cue #" & Number & " could not bumpt the status of cue #" & TargetCue, ex)
					End Try
				End If
			ElseIf Action = TransportActions.AutoPan Then
				If TargetCue > 0 Then
					Try
						My.Application.Show.Console.SendMessage("Transport Cue #" & Number & " is attempting an AutoPan action on cue #" & TargetCue)
						'My.Application.Show.AutoPanCueByNumber(Me, TargetCue)
						Dim q As Cues.CueBase = My.Application.Show.ShowFile.CueList.FindCuesList(Me.FullNumber).FindCueByNumber(TargetCue)
						If q Is Nothing Then Return
						If TypeOf q Is Cues.Audio Then
							If CType(q, Cues.Audio).Player IsNot Nothing AndAlso CType(q, Cues.Audio).Player.IsPlaying Then
								CType(q, Cues.Audio).AutoPan(PanDuration, TargetPan)
							Else
								Me.Stop()
								Return
							End If
						End If
					Catch ex As Exception
						If My.Application IsNot Nothing Then
							My.Application.Show.Console.SendMessage("Transport Cue #" & Number & " failed attempting an AutoPan action on cue #" & TargetCue)
						End If
					End Try
				End If
			ElseIf Action = TransportActions.AutoFade Then
				If TargetCue > 0 Then
					Try
						My.Application.Show.Console.SendMessage("Transport Cue #" & Number & " is attempting an AutoFade action on cue #" & TargetCue)
						'My.Application.Show.AutoFadeCueByNumber(Me, TargetCue)
						Dim q As Cues.CueBase = My.Application.Show.ShowFile.CueList.FindCuesList(Me.FullNumber).FindCueByNumber(TargetCue)
						If q Is Nothing Then Return
						If TypeOf q Is Cues.Audio Then
							If CType(q, Cues.Audio).Player IsNot Nothing AndAlso CType(q, Cues.Audio).Player.IsPlaying Then
								CType(q, Cues.Audio).AutoFade(FadeDuration, TargetFade)
							Else
								Me.Stop()
								Return
							End If
						End If
					Catch ex As Exception
						My.Application.Show.Console.SendMessage("Transport Cue #" & Number & " failed attempting an AutoFade action on cue #" & TargetCue)
					End Try
				End If
			End If

			Status = CueStatus.Standby
			MyBase.RaiseCueStopped(Me)

			Return

		End Sub

		''' <summary>
		''' Sets the cue's status to standby.
		''' </summary>
		''' <remarks></remarks>
		Public Overrides Sub Standby()
			Status = CueStatus.Standby
		End Sub

		''' <summary>
		''' Stops the cue. This includes stopping timers used for transport actions.
		''' </summary>
		''' <param name="Bool"></param>
		''' <remarks></remarks>
		Public Overrides Sub [Stop](Optional Bool As Boolean = True)
			Status = CueStatus.Standby
			RaiseCueStopped(Me, True)
			'If TransportActionTimer IsNot Nothing Then TransportActionTimer.Stop()
		End Sub

		''' <summary>
		''' Creates a new TransportCue, optionally specifying a parent cue.
		''' </summary>
		''' <param name="ParentCueNumber">The cue number of the parent cue for this TransportCue.</param>
		''' <remarks></remarks>
		Public Sub New(ParentCueNumber As String)
			MyBase.New(ParentCueNumber)
		End Sub

		''' <summary>
		''' Creates a new TransportCue.
		''' </summary>
		''' <remarks></remarks>
		Public Sub New()
			MyBase.New()
		End Sub
	End Class

	''' <summary>
	''' The types of actions a Transport Cue can perform.
	''' </summary>
	''' <remarks></remarks>
	Public Enum TransportActions As Integer
		StopSpecificCue
		StopAllActiveCues
		StartCue
		BumpCueStatus
		AutoPan
		AutoFade
	End Enum

End Namespace